#ifndef BACKGROUND_HPP
#define BACKGROUND_HPP

#define BACKGROUND_IMPLEMENTATION

#include "image.hpp"
#include "utils.h"

typedef struct Background {
  // Image
  Image image;

  // Move direction
  Direction direction;

  // Speed in pixel/second
  int speed;
} Background;

bool Background_load(SDL_Renderer *renderer, Background *background,
                     const char *file);
void Background_destroy(Background *background);

void Background_move(Background *background, int framerate);

void Background_render(SDL_Renderer *renderer, Background *background,
                       int width, int height);

#endif // header file protect

#ifdef BACKGROUND_IMPLEMENTATION
inline bool Background_load(SDL_Renderer *renderer, Background *background,
                     const char *file) {
  return Image_load(renderer, &background->image, file);
}
inline void Background_destroy(Background *background) {
  Image_destroy(&background->image);
}

inline void Background_move(Background *background, int framerate) {
  background->image.rect.y += background->speed / framerate;
  if (background->image.rect.y >= background->image.rect.h)
    background->image.rect.y =
        background->image.rect.y - background->image.rect.h;
}

inline void Background_render(SDL_Renderer *renderer, Background *background,
                       int width, int height) {
  // Unused width
  (void)width;

  SDL_Rect crop = background->image.rect;

  if (background->direction == DIRECTION_DOWN) {
    if (background->image.rect.y < height) {
      crop.y = 0;
      crop.h = background->image.rect.h - background->image.rect.y;

      SDL_Point pos = {0, background->image.rect.y};
      Image_renderCrop(renderer, &background->image, &crop, &pos);
    }

    if (background->image.rect.y != 0) {
      crop.y = background->image.rect.h - background->image.rect.y;
      crop.h = THIS_MIN(background->image.rect.y, height);

      SDL_Point pos = {0, 0};
      Image_renderCrop(renderer, &background->image, &crop, &pos);
    }
  }
}

#endif // do impl ?